package com.neusoft.nepb.common;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {
    public static Map readMapObject(String fileName){
        File file = new File(fileName);
        Map map = null;
        try{
            if(file.length()==0)
            {
                map=new HashMap();
            }else{
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                map = (Map)ois.readObject();
            }
        }catch(FileNotFoundException e){
            throw new RuntimeException(e);
        }catch(ClassNotFoundException e){
            throw new RuntimeException(e);
        }catch(IOException e){
            throw new RuntimeException(e);
        }
        return map;
    }

    public static void writeMapObject(String fileName, Map map){
        try {
            File file = new File(fileName);
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            oos.flush();
        }catch(IOException e){
            throw new RuntimeException(e);
        }
    }

}